<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名称：" prop="loginName">
        <el-input v-model="ruleForm.loginName"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="loginPwd" >
        <el-input   v-model="ruleForm.loginPwd" show-password></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          loginName: '',
          loginPwd: '',
        },
        rules: {
          loginName: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
          ],
          loginPwd: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],         
        },
      };
    },
    methods: {
      submitForm(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm.loginName,this.ruleForm.loginPwd);
              this.axios({
                url:"http://localhost:35043/api/User/LoginUsers",
                method:"post",
                data:{
                    loginName:this.ruleForm.loginName,
                    loginPwd:this.ruleForm.loginPwd,
                },
              })
            .then(
                (res)=>{
                    console.log(res.data);
                    if(res.data.code==200){
                        alert("登录成功！");
                    // 前端：浏览器接到,保存JWT
                    window.sessionStorage.setItem("token","Bearer "+res.data.tnToken.tokenStr);
                    window.sessionStorage.setItem('guid',res.data.guidss);
                    this.$router.push("/Product");
                    }else{
                        alert("登录失败！");
                        return;
                    }
                }
            )
         
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
